<template>
	<view class="main_container">
		<u-loading-page :loading="loading"/>
		<kevy-empty :show="!id_list.length"></kevy-empty>
		<view v-if="id_list.length">
			<post-message :is_own="true" v-for="id in id_list" :post="id" @del="del_Handle()"></post-message>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id_list: [],
				offset: 0,
				loading: false
			}
		},
		async onShow(){
			this.offset = 0
			let res_id_list = await this.load_posts()
			this.id_list = res_id_list
		},
		onReachBottom() {
			this.load_posts()
			.then(d => {
				d.forEach(oid => {
					this.id_list.push(oid)
				})
			})
		},
		methods: {
			async load_posts() {
				let id_list 
				try{
					id_list = await uni.request({
						url: getApp().globalData.base_url + "/get/post_message_list/self/" + this.offset,
						header: {token: getApp().globalData.access_token}
					})
					this.offset += id_list.data.length
					
				} catch(e) {
					throw e
				}
				
				return id_list.data
			},
			async del_Handle() {
				this.id_list = []
				this.offset = 0
				this.loading = true
				try{
					let res_id_list = await this.load_posts()
					this.loading = false
					this.id_list = res_id_list
					
				}catch(e){
					//TODO handle the exception
					console.log(e)
					this.loading = false
				}
			}
		}
	}
</script>

<style>
	.main_container {
		padding-top: 20px;
	}
</style>
